package com.syqc.monitor.ui.cost

import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import autodispose2.autoDispose
import com.syqc.adapter.ChoosePhotoAdapter
import com.syqc.comlib.custom.timer.CustomDatePicker
import com.syqc.comlib.custom.timer.DateFormatUtils
import com.syqc.comlib.rx.doAfterChanged
import com.syqc.comlib.rx.filterFast
import com.syqc.monitor.R
import com.syqc.monitor.databinding.FragmentInsureBinding
import com.syqc.monitor.databinding.IncludeDescPhotosBinding
import com.syqc.monitor.databinding.IncludeRecyclerWrapBinding
import com.syqc.monitor.databinding.IncludeSelectSureBinding
import com.syqc.utils.InputUtil
import com.syqc.utils.TimeUtil

class InsureFragment : BaseFeeFragment<FragmentInsureBinding>(FragmentInsureBinding::inflate) {
    private lateinit var descBinding: IncludeDescPhotosBinding
    private lateinit var mRecyclerVB: IncludeRecyclerWrapBinding
    private lateinit var sureBinding: IncludeSelectSureBinding
    private val mAdapter by lazy { ChoosePhotoAdapter() }
    private val mVM by viewModels<InsureViewModel>()
    override val mViewModel: BaseFeeViewModel
        get() = mVM
    override val isOil: Boolean
        get() = false
    override fun setDate(type: Int, time: String) {
        when (type) {
            0 -> {
                binding.lrvDate.setRightText(time)
                mVM.update = time
            }
            1 -> {
                binding.lrvDateStart.setRightText(time)
                mVM.startDate = time
            }
            2 -> {
                binding.lrvDateEnd.setRightText(time)
                mVM.endDate = time
            }
        }
    }

    override fun uploadData() {
        mVM.uploadInsure()
    }

    override fun clearData() {
        binding.uetCompany.setText("")
        binding.uetCode.setText("")
        binding.uetBusiness.setText("")
        binding.uetStrong.setText("")
        binding.uetTotal.setText("0.00")
        descBinding.editFeeRemark.setText("")
        mVM.clearData()
        binding.lrvDate.setRightText(mVM.update)
        binding.lrvDateStart.setRightText(mVM.startDate)
        binding.lrvDateEnd.setRightText(mVM.endDate)
        binding.nsView.fullScroll(View.FOCUS_UP)
    }

    override fun initView(view: View) {
        descBinding = IncludeDescPhotosBinding.bind(binding.root)
        sureBinding = IncludeSelectSureBinding.bind(binding.root)
        sureBinding.tvSure.setText(R.string.desc_com_sure)
        focusChange(binding.uetBusiness.getEditText())
        focusChange(binding.uetStrong.getEditText())
        binding.uetCompany.setFilter(15)
        binding.uetCode.setFilter(15)
        InputUtil.editTextNested(descBinding.editFeeRemark)

        binding.uetTotal.getEditText().isEnabled=false
        binding.lrvDate.setRightText(mVM.update)
        binding.lrvDateStart.setRightText(mVM.startDate)
        binding.lrvDateEnd.setRightText(mVM.endDate)

        mRecyclerVB = IncludeRecyclerWrapBinding.bind(binding.root)
        mRecyclerVB.recyclerWrap.layoutManager = GridLayoutManager(requireContext(), 3)
        mRecyclerVB.recyclerWrap.adapter = mAdapter
    }

    override fun initData() {
        super.initData()
        mVM.handlePhotos(null)

        mVM.feeTotalLive.observe(this) {
            binding.uetTotal.setText(it)
        }

        mVM.photoLive.observe(this) {
            mAdapter.setList(it)
            mAdapter.notifyDataSetChanged()
        }
    }

    override fun initEvent() {

        binding.uetCompany.getEditText().doAfterChanged {
            mVM.insureCom = it
        }
        binding.uetCode.getEditText().doAfterChanged {
            mVM.insureNum = it
        }
        binding.uetBusiness.setOnTextChange {
            val str = isNumOk(binding.uetBusiness, it)
            mVM.business = str
        }

        binding.uetStrong.setOnTextChange {
            val str = isNumOk(binding.uetStrong, it)
            mVM.strong = str
        }
        sureBinding.tvSure.filterFast {
            mVM.uploadInsure()
        }
        descBinding.editFeeRemark.doAfterChanged {
            mVM.remark = it
        }

        mAdapter.setOnItemClickListener { adapter, view, position ->
            val path = mAdapter.data[position]
            if (path.isEmpty()) {
                takePhoto()
            }
        }

        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            mViewModel.removePhoto(position)
        }

        binding.lrvDate.setOnClickListener {
            mVM.dataType = 0
            mDatePicker2.show(mVM.update)
        }

        binding.lrvDateStart.setOnClickListener {
            mVM.dataType = 1
            mDatePicker2.show(mVM.startDate)
        }

        binding.lrvDateEnd.setOnClickListener {
            mVM.dataType = 2
            mDatePicker2.show(mVM.startDate)
        }
    }

    private val mDatePicker2: CustomDatePicker by lazy {
        CustomDatePicker(
            requireContext(), callback2, TimeUtil.simpleDate,
            TimeUtil.simpleDateEnd, false
        ).apply {
            setCanShowPreciseTime(false)
        }
    }
    private val callback2: CustomDatePicker.Callback = object : CustomDatePicker.Callback {
        override fun onTimeSelected(timestamp: Long) {
            val time: String = DateFormatUtils.long2Str(timestamp, false)
            setDate(mViewModel.dataType, time)
            mDatePicker2.dismiss()
        }
    }
}